﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Assets.Scripts.TDu3DWork
{
    public class TDuAudioPlayer : MonoBehaviour
    {
        TDuAudioPlayer Instance;
        private AudioSource m_Aud;
        void Start()
        {
            m_Aud = GameObject.Find("AudioSourceMgr").GetComponent<AudioSource>();
        }
        // Start is called before the first frame update
        public void PlayAudio(string path)
        {
            StartCoroutine(DownloadPlayAudio( path));
        }
        public IEnumerator DownloadPlayAudio(string path)
        {
            Debug.Log(path);
            if (!string.IsNullOrEmpty(path))
            {
                using (var uwr = UnityWebRequestMultimedia.GetAudioClip(Application.streamingAssetsPath + "/" + TDuMainEntry.m_profile.resourcePath + path, AudioType.MPEG))
                {
                    yield return uwr.SendWebRequest();
                    if (!(uwr.isNetworkError || uwr.isHttpError))
                    {
                        m_Aud.clip = DownloadHandlerAudioClip.GetContent(uwr);
                    }
                    else m_Aud.clip = null;
                }
            }
            else
            {
                m_Aud.clip = null;
            }
            m_Aud.Play();
            yield return 0;
        }
    }
}
